using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ORSLibrary.Service;
using ORSLibrary.Common;
using System.Web;

namespace ORSLibrary.DAL
{
	public partial class Schedule : BusinessEntity
	{
        #region Public Properties
		
        public int ID { get; set; }
		public int CompanyID { get; set; }
		public int ProgramID { get; set; }
		public int AssemblyLocationID { get; set; }
		public int MeccaDays { get; set; }
		public int MadinaDays { get; set; }
		public bool IsMadinaFirst { get; set; }
		public byte TransportationType { get; set; }
		public DateTime TravelDateTime { get; set; }
		public decimal BasicPrice { get; set; }
		public int CreatedBy { get; set; }
		public DateTime CreatedDate { get; set; }
		public int ModifiedBy { get; set; }
		public DateTime ModifiedDate { get; set; }
		public bool IsDeleted { get; set; }
		
        #endregion
        
		public Schedule()
		{
            IsNew = true;
		}
		
		public Schedule(int ID)
		{
			SqlService sql = new SqlService();
			sql.AddParameter("@ID", SqlDbType.Int, ID);
			SqlDataReader reader = sql.ExecuteSPReader("ScheduleSelect");
			
			if (reader.Read()) 
			{
				this.LoadFromReader(reader);
				reader.Close();
			}
			else
			{
				if (!reader.IsClosed) reader.Close();
				throw new ApplicationException("Schedule does not exist.");
			}
		}
		
		public Schedule(SqlDataReader reader)
		{
			this.LoadFromReader(reader);
		}
		
		protected void LoadFromReader(SqlDataReader reader)
		{
			if (reader != null && !reader.IsClosed)
			{
				ID = (int)reader["ID"];
                
				CompanyID = reader["CompanyID"] == DBNull.Value ? 0 : (int)reader["CompanyID"];
				ProgramID = reader["ProgramID"] == DBNull.Value ? 0 : (int)reader["ProgramID"];
				AssemblyLocationID = reader["AssemblyLocationID"] == DBNull.Value ? 0 : (int)reader["AssemblyLocationID"];
				MeccaDays = reader["MeccaDays"] == DBNull.Value ? 0 : (int)reader["MeccaDays"];
				MadinaDays = reader["MadinaDays"] == DBNull.Value ? 0 : (int)reader["MadinaDays"];
				IsMadinaFirst = reader["IsMadinaFirst"] == DBNull.Value ? false : (bool)reader["IsMadinaFirst"];
				TransportationType = reader["TransportationType"] == DBNull.Value ? Byte.MinValue : (byte)reader["TransportationType"];
				TravelDateTime = reader["TravelDateTime"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["TravelDateTime"];
				BasicPrice = reader["BasicPrice"] == DBNull.Value ? 0 : (decimal)reader["BasicPrice"];
				CreatedBy = reader["CreatedBy"] == DBNull.Value ? 0 : (int)reader["CreatedBy"];
				CreatedDate = reader["CreatedDate"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["CreatedDate"];
				ModifiedBy = reader["ModifiedBy"] == DBNull.Value ? 0 : (int)reader["ModifiedBy"];
				ModifiedDate = reader["ModifiedDate"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["ModifiedDate"];
				IsDeleted = reader["IsDeleted"] == DBNull.Value ? false : (bool)reader["IsDeleted"];
                
                IsNew = false;
			}
		}
		
		public void Delete()
		{
			Schedule.Delete(ID);
		}
        
        public void Save()
		{
            if(IsNew)
                Create();
            else
                Update();
        }
		
		public void Update()
		{
            ModifiedBy = (int)HttpContext.Current.Session["UserID"];

            SqlService sql = new SqlService();

			sql.AddParameter("@ID", SqlDbType.Int, ID);			
            FillParamters(sql);
		
			sql.ExecuteSP("ScheduleUpdate");
		}
		
		public void Create()
		{
            CreatedBy = (int)HttpContext.Current.Session["UserID"];
            ModifiedBy = (int)HttpContext.Current.Session["UserID"];
            
			SqlService sql = new SqlService();

            FillParamters(sql);
			
			sql.ExecuteSP("ScheduleInsert");
            
            ID = (int)sql.Parameters["@ID"].Value;
		}
        
        public void FillParamters(SqlService sql)
		{
			sql.AddParameter("@CompanyID", SqlDbType.Int, CompanyID);
			sql.AddParameter("@ProgramID", SqlDbType.Int, ProgramID);
			sql.AddParameter("@AssemblyLocationID", SqlDbType.Int, AssemblyLocationID);
			sql.AddParameter("@MeccaDays", SqlDbType.Int, MeccaDays);
			sql.AddParameter("@MadinaDays", SqlDbType.Int, MadinaDays);
			sql.AddParameter("@IsMadinaFirst", SqlDbType.Bit, IsMadinaFirst);
			sql.AddParameter("@TransportationType", SqlDbType.TinyInt, TransportationType);
			sql.AddParameter("@TravelDateTime", SqlDbType.DateTime, TravelDateTime);
			sql.AddParameter("@BasicPrice", SqlDbType.Money, BasicPrice);
			sql.AddParameter("@CreatedBy", SqlDbType.Int, CreatedBy);
			sql.AddParameter("@CreatedDate", SqlDbType.DateTime, CreatedDate);
			sql.AddParameter("@ModifiedBy", SqlDbType.Int, ModifiedBy);
			sql.AddParameter("@ModifiedDate", SqlDbType.DateTime, ModifiedDate);
			sql.AddParameter("@IsDeleted", SqlDbType.Bit, IsDeleted);
        }
		
		public static Schedule NewEntity(int ID)
		{
			Schedule newEntity = new Schedule();
			newEntity.ID = ID;

			return newEntity;
		}
		
        public static Schedule GetEntity(int ID)
		{
			return new Schedule(ID);
		}
		
		public static void Delete(int ID)
		{
			SqlService sql = new SqlService();
			sql.AddParameter("@ID", SqlDbType.Int, ID);
            sql.AddParameter("@ModifiedBy", SqlDbType.Int, HttpContext.Current.Session["UserID"]);
	
			sql.ExecuteSP("ScheduleDelete");
		}
        
        public static ScheduleList GetList(bool isComboBox)
		{
			ScheduleList list = new ScheduleList();
			
            SqlService sql = new SqlService();
			SqlDataReader reader = sql.ExecuteSPReader("ScheduleSelectAll");
			
			while (reader.Read()) 
			{
                list.Add(new Schedule(reader));
			}
            reader.Close();
            
			return list;
		}
	}
    public partial class ScheduleList : List<Schedule>
	{
    }
}
